/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 
package com.cburch.logisim.circuit;

import java.awt.Color;
import java.awt.Font;
import java.util.Arrays;
import java.util.List;

import com.cburch.logisim.data.AbstractAttributeSet;
import com.cburch.logisim.data.Attribute;
import com.cburch.logisim.data.Attributes;
import com.cburch.logisim.data.Direction;
import com.cburch.logisim.instance.StdAttr;
import com.cburch.logisim.util.Strings;

class CircuitAttributes extends AbstractAttributeSet {
    private static final Attribute NAME_ATTR = Attributes.forString("circuit", Strings.getter("circuitName", Strings.circuitSource));
    private static final Attribute LABEL_DIR = Attributes.forDirection("labelDir", Strings.getter("ioLabelLocAttr", Strings.circuitSource));
    private static final Attribute ATTR_LABEL_COLOR = Attributes.forColor("labelcolor", Strings.getter("ioLabelColorAttr", Strings.circuitSource));
    private static final List ATTRIBUTES = Arrays.asList(new Attribute[] { StdAttr.FACING, NAME_ATTR, StdAttr.LABEL, LABEL_DIR, StdAttr.LABEL_FONT, ATTR_LABEL_COLOR });

    private Circuit source;
    private Subcircuit comp;
    private Direction facing = Direction.EAST;
    private Direction labelDirection = Direction.NORTH; 
    private String label = "";
    private Font font = StdAttr.DEFAULT_LABEL_FONT; 
    private Color color = Color.black;
    
    public CircuitAttributes(Circuit source) {
        this.source = source;
    }
    
    void setSubcircuit(Subcircuit value) {
        comp = value;
    }
    
    public Direction getFacing() {
        return facing;
    }
    
    public String getLabel()
    {
    	return label;
    }
    
    public Direction getLabelDirection()
    {
    	return labelDirection;
    }
    
    public Font getFont()
    {
    	return font;
    }
    
    public Color getColor()
    {
    	return color;
    }

    protected void copyInto(AbstractAttributeSet dest) {
        CircuitAttributes other = (CircuitAttributes) dest;
        other.comp = null;
    }
    
    public boolean isReadOnly(Attribute attr) {
        if (attr == StdAttr.LABEL) return false;
        if (attr == LABEL_DIR) return false;
        if (attr == StdAttr.LABEL_FONT) return false;
        if (attr == ATTR_LABEL_COLOR) return false;
    	
    	return comp != null || attr == NAME_ATTR;
    }

    public List getAttributes() {
        return ATTRIBUTES;
    }

    public Object getValue(Attribute attr) {
        if(attr == StdAttr.FACING) return facing;
        if(attr == NAME_ATTR) return source.getName();
        if(attr == StdAttr.LABEL) return label;
        if(attr == LABEL_DIR) return labelDirection;
        if(attr == StdAttr.LABEL_FONT) return font;
        if(attr == ATTR_LABEL_COLOR) return color;
        return null;
    }

    public void setValue(Attribute attr, Object value) {
        if(attr == StdAttr.FACING) {
            facing = (Direction) value;
            fireAttributeValueChanged(StdAttr.FACING, value);
        }
        else if (attr == StdAttr.LABEL){
        	label = (String)value;
        }
        else if (attr == LABEL_DIR){
        	labelDirection = (Direction)value;
        }
        else if (attr == StdAttr.LABEL_FONT){
        	font = (Font)value;
        }
        else if (attr == ATTR_LABEL_COLOR){
        	color = (Color)value;
        }
    }
}
